package oj.hot100;

public class 目标和 {
    //01背包问题,优化前
    public int findTargetSumWaysPre(int[] nums, int target) {
        int sum = 0,n = nums.length;
        for(int i = 0;i < n;i++) {
            sum += nums[i];
        }
        int a = (sum+target)/2;
        //处理a的边界情况,
        if((sum+target) % 2 == 1 || a < 0) {
            return 0;
        }
        int[][] dp = new int[n+1][a+1];
        dp[0][0] = 1;
        for(int i = 1;i <= n;i++) {
            for(int j = 0;j <= a;j++) {
                dp[i][j] = dp[i-1][j];
                if(j >= nums[i-1]) {
                    dp[i][j] += dp[i-1][j-nums[i-1]];
                }
            }
        }
        return dp[n][a];
    }
    //优化后

}
